package com.igeek.shop.dao.impl;

import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.taglibs.standard.tag.common.sql.DataSourceUtil;

import com.igeek.shop.dao.IUserDAO;
import com.igeek.shop.entity.User;
import com.igeek.utils.DataSourceUtils;

public class UserDAOImpl implements IUserDAO{

	@Override
	public int insert(User user) throws SQLException 
	{
		String sql = "insert into user values(?,?,?,?,?,?,?,?,?,?)";
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		
		Object[] params = {user.getUid(),user.getUsername(),user.getPassword(),
				user.getName(),user.getEmail(),user.getTelephone(),
				df.format(user.getBirthday()),user.getSex(),user.getState(),user.getCode()};
		
		int result  = runner.update(sql,params);
		return result;
	}
	@Override
	public User findByUsername(String username) throws SQLException {
		String sql = "select * from user where username = ?;";
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		User user = runner.query(sql, new BeanHandler<>(User.class),username);
		return user;
	}

}
